/*
 * @Author: DUST
 * @Date: 2023-05-16 14:41:06
 * @LastEditors: DUST
 * @LastEditTime: 2023-05-16 14:45:48
 * @FilePath: \vite-project\src\baseClass\table.ts
 * @Description: 
 * undefined
 */

import { Ajax, PageInterFace, PageRes } from "@/interface";
import { AxiosResponse } from "axios";
import { Ref, ref, watch } from "vue";

interface tableIndex {
  index: number;
}
class BaseTable<T extends tableIndex> {
  /**
   * @description: 表格数据
   */
  private _tableData = ref<Array<T>>([]) as Ref<T[]>;
  public get tableData(): Ref<T[]> {
    return this._tableData;
  }
  /**
   * @description: 选择列表项
   */
  private _rowskeys = ref<Array<number>>([]) as Ref<number[]>;
  public get rowskeys(): Ref<number[]> {
    return this._rowskeys;
  }
  /**
   * @description: 选择列表项
   */
  private _selectedRows = ref<Array<number>>([]) as Ref<number[]>;
  public get selectedRows(): Ref<number[]> {
    return this._selectedRows;
  }
  /**
   * @description: 表格loading加载
   */
  private _tableLoading = ref<boolean>(false) as Ref<boolean>;
  public get tableLoading(): Ref<boolean> {
    return this._tableLoading;
  }
  /**
   * @description: 模糊查询
   */
  private _vague = ref<string>("") as Ref<string>;
  public get vague(): Ref<string> {
    return this._vague;
  }
  /**
   * @description: 排序筛选
   */
  private sortAndFilter = ref({
    orderList: [],
    screeningList: []
  });
  /**
   * @description: 输入框中模糊查询的字段
   */
  private _searchKeys: Array<string> = ["name", "number"];
  public set searchKeys(v: Array<string>) {
    this._searchKeys = v;
  }

  /**
   * @description: 按钮默认方法
   */
  public baseBtnFunc = () => undefined;

  /**
   * @description: 分页初始数据
   */
  private _pagination = ref<PageInterFace>({
    current: 1, //初始页
    pageNumber: 1,
    pageSize: 10, //分页大小
    showTotal: (total: any) => `共${total}条数据`,
    showQuickJumper: true,
    showSizeChanger: true
  });
  public get pagination(): Ref<PageInterFace> {
    return this._pagination;
  }
  /**
   * @description: 表格接口
   */
  private _AJAXFunc: (
    page: PageInterFace
  ) => Promise<AxiosResponse<Ajax<PageRes<T>>>>;

  // public set AJAXFunc(
  //   v: (page: PageInterFace) => Promise<AxiosResponse<Ajax<PageRes<T>>>>
  // ) {
  //   this._AJAXFunc = v;
  // }

  /**
   * @description: 回调函数
   */
  private callBackFunc:
    | ((data: Array<T>, res: AxiosResponse<Ajax<PageRes<T>>>) => void)
    | undefined;
  /**
   * @description: 默认参数
   */
  private params: PageInterFace = this._pagination.value;
  /**
   * @description: 是否监听参数变化
   */
  private _watchParams: boolean;
  public get watchParams(): boolean {
    return this._watchParams;
  }

  /**
   * @description: 构造函数初始化
   * @params @options AJAXFunc: 表格接口
   * @params @options callBackFunc: 表格接口成功回调函数
   */
  constructor({
    params = {},
    AJAXFunc,
    callBackFunc,
    watchParams = true
  }: {
    params?: PageInterFace;
    AJAXFunc: (page: PageInterFace) => Promise<AxiosResponse<Ajax<PageRes<T>>>>;
    callBackFunc?: (
      data: Array<T>,
      res?: AxiosResponse<Ajax<PageRes<T>>>
    ) => void;
    watchParams?: boolean;
  }) {
    this._AJAXFunc = AJAXFunc;
    this.callBackFunc = callBackFunc;
    this._watchParams = watchParams;
    if (params) this.params = { ...params };
    this.getData();
    if (this._watchParams) {
      watch(this._pagination, (value1, value2) => {
        console.log(value1, value2);
        this.getData();
      });
    }
  }

  /**
   * @description: 获取表格数据
   * @params params: 表格接口参数
   */
  public getData = (params: PageInterFace = this._pagination.value) => {
    // console.log("调用getData");
    const obj = { ...this.params, ...params };
    this._tableLoading.value = true;
    this._AJAXFunc(obj)
      .then((res: AxiosResponse<Ajax<PageRes<T>>>) => {
        res.data.data.list.forEach((val: T, i: number) => {
          val.index = i + 1;
        });
        this._tableData.value = res.data.data.list;
        this._pagination.value.showTotal = (total: any) => `共${total}条数据`;
        this._pagination.value.total = res.data.data.total;
        if (this.callBackFunc) this.callBackFunc(res.data.data.list, res);
      })
      .finally(() => {
        this._tableLoading.value = false;
      });
  };
  /**
   * @description: 切换参数，清空分页重新获取表格数据
   * @param {PageInterFace} params：切换的参数
   */
  public changeParamsCleanPage = (params: PageInterFace = {}) => {
    const obj = {
      ...this.params,
      pageNumber: 1,
      current: 1,
      total: this._pagination.value.total,
      ...params
    };
    // console.log(obj, "参数");
    this._pagination.value = obj;
    if (!this.watchParams) {
      this.getData(this._pagination.value);
    }
  };

  /**
   * @description: 表格顶部输入搜索
   */
  // public search = () => {
  //   const obj = getVagueJson(
  //     this._vague.value,
  //     this.sortAndFilter.value,
  //     this._searchKeys
  //   );
  //   this._pagination.value.vagueJson = obj.JsonScreeningList;
  //   this._pagination.value.screeningJson = obj.JsonObj;
  //   this.changeParamsCleanPage(this._pagination.value);
  // };
}

export default BaseTable;
